#include <iostream>
#include <cstring>
#define MAXN 105
#define MAXW 10005
using namespace std;

int N, w[MAXN], v[MAXN], W;
int dp[MAXN][MAXW];

void solve();
int rec(int i, int j);
int main() {
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> w[i] >> v[i];
    cin >> W;
    // solve();
    memset(dp, -1, sizeof(dp));
    cout << rec(N, W) << endl;
    return 0;
}



/*
** DP[i][j]从前i个挑重量为j的最大价值
** DP[i][j] = DP[i - 1][j - w[i]] + v[i] | DP[i - 1][j]
** 采用循环的方式计算
*/
void solve() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= W; j++) {
            if (w[i] <= j) dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
            else dp[i + 1][j] = dp[i][j];
        }
    }
    cout << dp[N][W] << endl;
}

/* 
* 采用递归的方式进行计算:
*/

int rec(int i, int j) {
    if (dp[i][j] >= 0) return dp[i][j];
    if (i == 0) return dp[i][j] = 0;
    if (j == 0) return dp[i][j] = 0;
    if (j >= w[i - 1]) return dp[i][j] = max(rec(i - 1, j - w[i - 1]) + v[i - 1], rec(i - 1, j));
    else return dp[i][j] = rec(i - 1, j);
}